import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/layout/index.vue'

Vue.use(VueRouter)

// 解决重复点击路由的时候报错
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

export const routes = [
  {
    path: '/',
    redirect: '/sample-management'
  },

  // 登录
  {
    path: '/login',
    component: () => import('@/views/login/index.vue'),
    hidden: true
  },
  // 样本管理
  {
    path: '/sample-management',
    component: Layout,
    redirect: '/sample-management/decision',
    children: [
      {
        path: '/sample-management/decision',
        component: () => import('@/views/sampleManagement/decision/index.vue'),
        meta: { title: '决策样本管理', keepAlive: false }
      },

      {
        path: '/sample-management/taskCase',
        component: () => import('@/views/sampleManagement/taskCase/index.vue'),
        meta: { title: '任务案例管理', keepAlive: false }
      },
      {
        path: '/sample-management/statistical',
        component: () =>
          import('@/views/sampleManagement/statistical/index.vue'),
        meta: { title: '样本统计展示', keepAlive: false }
      }
    ]
  },
  // 模型管理
  {
    path: '/model-management',
    component: Layout,
    redirect: '/model-management/decisionModel-model',
    children: [
      {
        path: '/model-management/decisionModel-model',
        component: () =>
          import('@/views/modelManagement/decisionModel/index.vue'),
        meta: { title: '决策模型管理', keepAlive: false }
      },
      {
        path: '/model-management/decisionModel-model/train',
        component: () =>
          import('@/views/modelManagement/decisionModel/train/index.vue'),
        meta: { title: '决策模型管理-训练', keepAlive: false }
      },
      {
        path: '/model-management/mission-planning',
        component: () =>
          import('@/views/modelManagement/missionPlanning/index.vue'),
        meta: { title: '任务规划模型管理', keepAlive: false }
      },
      {
        path: '/model-management/mission-planning/train',
        component: () =>
          import('@/views/modelManagement/missionPlanning/train/index.vue'),
        meta: { title: '任务规划模型管理-训练', keepAlive: false }
      },
      {
        path: '/model-management/user-management',
        component: () =>
          import('@/views/modelManagement/userManagement/index.vue'),
        meta: { title: '用户管理', keepAlive: false }
      }
    ]
  },
  // 业务运行调度
  {
    path: '/business-management',
    component: Layout,
    redirect: '/business-management/demand-management',
    children: [
      {
        path: '/business-management/demand-decision',
        component: () =>
          import('@/views/businessManagement/demandDecision/index.vue'),
        meta: { title: '需求决策', keepAlive: false }
      },
      {
        path: '/business-management/task-planning',
        component: () =>
          import('@/views/businessManagement/taskPlanning/index.vue'),
        meta: { title: '任务规划', keepAlive: false }
      },
      {
        path: '/business-management/assessment',
        component: () =>
          import('@/views/businessManagement/assessment/index.vue'),
        meta: { title: '评估', keepAlive: false }
      }
    ]
  },
  // 知识图谱管理
  {
    path: '/knowledge-management',
    component: Layout,
    redirect: '/knowledge-management/knowledge-addition',
    children: [
      {
        path: '/knowledge-management/knowledge-addition',
        component: () =>
          import('@/views/knowledgeManagement/knowledgeAddition/index.vue'),
        meta: { title: '知识添加', keepAlive: false }
      },
      {
        path: '/knowledge-management/knowledge-query',
        component: () =>
          import('@/views/knowledgeManagement/knowledgeQuery/index.vue'),
        meta: { title: '知识查询', keepAlive: false }
      },
      {
        path: '/knowledge-management/knowledge-type-addition',
        component: () =>
          import('@/views/knowledgeManagement/knowledgeTypeAddition/index.vue'),
        meta: { title: '知识类型添加', keepAlive: false }
      },
      {
        path: '/knowledge-management/reasoning-application',
        component: () =>
          import('@/views/knowledgeManagement/ReasoningApplication/index.vue'),
        meta: { title: '推理应用', keepAlive: false }
      }
    ]
  },
  // 需求与资源管理
  {
    path: '/manage',
    component: Layout,
    redirect: '/manage/demand-management',
    children: [
      {
        path: '/manage/demand-management',
        component: () =>
          import('@/views/businessManagement/demandManagement/index.vue'),
        meta: { title: '需求管理', keepAlive: false }
      },
      {
        path: '/manage/resource-management',
        component: () =>
          import('@/views/businessManagement/resourceManagement/index.vue'),
        meta: { title: '资源管理', keepAlive: false },
        children: [
          {
            path: '/manage/observation',
            component: () =>
              import(
                '@/views/businessManagement/resourceManagement/observation/index.vue'
              ),
            meta: { title: '观测卫星管理', keepAlive: false }
          },
          {
            path: '/manage/groundStation',
            component: () =>
              import(
                '@/views/businessManagement/resourceManagement/groundStation/index.vue'
              ),
            meta: { title: '地面站管理', keepAlive: false }
          }
        ]
      }
    ]
  }
]

export const resetRouter = () => {}

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (window.__axiosCancelTokenArr) {
    window.__axiosCancelTokenArr.forEach(ele => {
      ele.cancel('取消请求')
    })
    window.__axiosCancelTokenArr = []
  }
  next()
})

export default router
